package com.sk.ccflower.server.messenger.xmlrpc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sk.ccflower.lib.commonBeans.DefaultParamBean;
import com.sk.ccflower.lib.commonBeans.XmlRpcResultBean;
import com.sk.ccflower.lib.commonUtils.ObjectUtil;
import com.sk.ccflower.lib.commonUtils.PhotoUtil;
import com.sk.ccflower.lib.define.Consts;
import com.sk.ccflower.lib.meBeans.BlockUserBean;
import com.sk.ccflower.lib.meBeans.MessageBean;
import com.sk.ccflower.lib.meBeans.MessageSummaryBean;
import com.sk.ccflower.lib.meBeans.ShareInfoBean;
import com.sk.ccflower.server.common.xmlRpcManager.BaseXmlRpcWorker;
import com.sk.ccflower.server.lib.dbBeans.CfProfileBean;
import com.sk.ccflower.server.lib.dbBeans.CfProfileShareBean;
import com.sk.ccflower.server.lib.dbBeans.CfSpamListBean;
import com.sk.ccflower.server.lib.dbBeans.MfCommentBean;
import com.sk.ccflower.server.lib.dbUtils.ConnectionPoolManager;
import com.sk.ccflower.server.lib.dbUtils.DBConnection;
import com.sk.ccflower.server.lib.dbUtils.qry.CfProfileQry;
import com.sk.ccflower.server.lib.dbUtils.qry.CfProfileShareQry;
import com.sk.ccflower.server.lib.dbUtils.qry.CfSpamListQry;
import com.sk.ccflower.server.lib.dbUtils.qry.MfCommentQry;

public class MessengerCenterXmlRpc extends BaseXmlRpcWorker {
	
	private Logger m_log;
	
	public MessengerCenterXmlRpc(ConnectionPoolManager dbcp) {
		super(dbcp);
		
		this.m_log = Logger.getLogger( getClass() );
	}
	
	public XmlRpcResultBean messageSummaryWork( String email ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Iterator<String> it = null;
		String key = null;
		MessageSummaryBean sumBean = null;
		int msgTime = 0;
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			MfCommentQry qry = new MfCommentQry(dbconn);
			qry.select( email );
			CfProfileQry pQry = new CfProfileQry(dbconn);
			pQry.select();
			
			for ( int i=0; i<qry.m_arr.size(); i++ ) {
				MfCommentBean bean = (MfCommentBean) qry.m_arr.get(i);
				
				msgTime = (int) (bean.c_date.getTime() / 1000);
				
				if ( bean.src_user.equals(email) ) {
					key = bean.dest_user;
				} else {
					key = bean.src_user;
				}
				
				if ( dataMap.containsKey(key) ) {
					sumBean = (MessageSummaryBean) dataMap.get(key);
					sumBean.msg_cnt++;
					if ( msgTime > sumBean.last_msg_time )	sumBean.last_msg_time = msgTime;
				} else {
					sumBean = new MessageSummaryBean();
					sumBean.email = key;
					sumBean.msg_cnt++;
					sumBean.last_msg_time = msgTime;
					dataMap.put( key, sumBean );
				}
			}
			
			it = dataMap.keySet().iterator();
			
			ArrayList<MessageSummaryBean> tmpList = new ArrayList<MessageSummaryBean>();
			
			while ( it.hasNext() ) {
				key = it.next();
				sumBean = (MessageSummaryBean) dataMap.get( key );
				CfProfileBean pBean = (CfProfileBean) pQry.get(sumBean.email);
				if ( null == pBean ) {
					dataMap.remove( key );
					continue;
				}
				
				sumBean.alias = pBean.alias;
				sumBean.img_profile = PhotoUtil.getDefaultPhoto( sumBean.email, Consts.PHOTO_TYPE.PROFILE );
				
				//retBean.resultList.add( sumBean );
				tmpList.add( sumBean );
			}
			
			// 최근순으로 정렬
			Comparator<MessageSummaryBean> comp = new Comparator<MessageSummaryBean>() {
				@Override
				public int compare( MessageSummaryBean o1, MessageSummaryBean o2 ) {
					return (o1.last_msg_time > o2.last_msg_time) ? -1 : (o1.last_msg_time == o2.last_msg_time) ? 0 : 1;
				}
			};
			Collections.sort( tmpList, comp );
			retBean.resultList.addAll( tmpList );
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean messageListWork( DefaultParamBean param ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		MessageBean mBean = null;
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			MfCommentQry qry = new MfCommentQry(dbconn);
			qry.select( param.src_user, param.dest_user );
			
			for ( int i=0; i<qry.m_arr.size(); i++ ) {
				MfCommentBean bean = (MfCommentBean) qry.m_arr.get(i);
				
				mBean = new MessageBean();
				ObjectUtil.objectCopy( mBean, bean, false );
				
				retBean.resultList.add( mBean );
			}
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean shareInfoWork( ShareInfoBean bean ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		MessageBean mBean = null;
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			CfProfileShareQry qry = new CfProfileShareQry(dbconn);
			if ( !bean.share ) {
				qry.delete( bean );
			} else {
				CfProfileShareBean dbBean = new CfProfileShareBean();
				ObjectUtil.objectCopy( dbBean, bean, false );
				qry.insert( dbBean );
			}
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean blockUserListWork( String email ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		BlockUserBean bBean = null;
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			CfSpamListQry qry = new CfSpamListQry( dbconn );
			qry.selectAttacker( email );
			CfProfileQry pQry = new CfProfileQry( dbconn );
			pQry.select();
			
			for ( int i=0; i<qry.m_arr.size(); i++ ) {
				CfSpamListBean bean = (CfSpamListBean) qry.m_arr.get(i);
				
				bBean = new BlockUserBean();
				ObjectUtil.objectCopy( bBean, bean, false );
				
				CfProfileBean pBean = (CfProfileBean) pQry.get( bBean.dest_user );
				if ( null == pBean ) {
					continue;
				}
				
				bBean.alias = pBean.alias;
				
				retBean.resultList.add( bBean );
			}
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean blockUserWork( String src_user, String dest_user ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			CfSpamListQry qry = new CfSpamListQry( dbconn );
			CfSpamListBean dbBean = new CfSpamListBean();
			dbBean.src_user = src_user;
			dbBean.dest_user = dest_user;
			qry.insert( dbBean );
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean unblockUserWork( String email, String idxs ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			CfSpamListQry qry = new CfSpamListQry( dbconn );
			qry.delete( email, idxs );
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
	public XmlRpcResultBean initMessageWork( String src_user, String dest_user ) throws Exception {
		DBConnection dbconn = null;
		XmlRpcResultBean retBean = new XmlRpcResultBean();
		
		try {
			retBean.resultList = new Vector<Object>();
			dbconn = new DBConnection( super.mConnectionPool.getConnection() );
			MfCommentQry qry = new MfCommentQry( dbconn );
			qry.delete( src_user, dest_user );
			
			retBean.resultMsg = "SUCCESS";
		} catch ( Exception e ) {
			retBean.resultCode = XmlRpcResultBean.XML_RPC_RESULT_FAILURE;
			retBean.resultMsg = "FAILUE";
			this.m_log.error( e );
			e.printStackTrace();
			throw e;
		} finally {
			if ( null != dbconn )	dbconn.close();
		}
		
		return retBean;
	}
	
}
